package com.imay.live.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.Constants;
import com.imay.live.activity.RoomPushNormalActivity;
import com.imay.live.activity.RoomPushShortActivity;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.helper.MediaHelper;
import com.imay.live.view.custom.MainActionView;



/**
 * Created by jeaman on 2017/3/15.
 * 首页底部action按钮
 */

public class MainActionButtonFragment extends BaseDialogFragment implements View.OnClickListener, MainActionView.OnActionClickListener {
    private MainActionView mActionButtonView;

    public static MainActionButtonFragment newInstance() {
        return new MainActionButtonFragment();
    }

    @Override
    protected View getCustomView(Context context) {
        mActionButtonView = new MainActionView(context);
        return mActionButtonView;
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActionButtonView.setOnClickListener(this);
        mActionButtonView.setOnActionClickListener(this);
        mActionButtonView.showActionButton();
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v == mActionButtonView) {
            mActionButtonView.hideActionButton(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            });
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    @Override
    public void onActionClick(int position) {
        dismiss();
        switch (position) {
            case 0:
                RoomPushShortActivity.startActivity(getActivity());
                MobclickAgent.onEvent(getActivity(),"action_cdb");
                break;
            case 1:
                RoomPushNormalActivity.startActivity(getActivity());
                MobclickAgent.onEvent(getActivity(),"action_click_start");
                break;
            case 2:
                MediaHelper.startPhoto(getActivity(), Constants.REQUEST_PICK_IMAGE);
                MobclickAgent.onEvent(getActivity(),"action_click_photo");

                break;
            case 3:
                MediaHelper.startRecord(getActivity());
                MobclickAgent.onEvent(getActivity(),"action_click_video");
                break;
        }
    }

    boolean isCreated;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreated = true;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // 友盟统计页面跳转，重写此方法，可以保证ViewPager切换Fragment时能够准确的记录Fragment之间的跳转
        // 不用再调用Fragment的生命周期方法
        if (isCreated){
            if (isVisibleToUser) {
                MobclickAgent.onPageStart(getClass().getSimpleName());
            } else {
                MobclickAgent.onPageEnd(getClass().getSimpleName());
            }
        }
    }
}
